package com.nCoV.EpidemicSurveillance.common.utils;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 罗穆健
 * @date 2020年4月21日
 */
public class ListUtil {
    public static void ListSort(List<Map<String, Object>> list,String name) {
    //用Collections这个工具类传list进来排序
    	if(list.isEmpty()) return;
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                try {
                    Date dt1 = format.parse(o1.get(name).toString());
                    Date dt2 = format.parse(o2.get(name).toString());
                    
                    if (dt1.getTime() > dt2.getTime()) {
                    	
                        return 1;//小的放前面
                    }else {
                    	
                        return -1;
                    } 
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
    public static void ListSortDay(List<Map<String, Object>> list,String name) {
        //用Collections这个工具类传list进来排序
        	if(list.isEmpty()) return;
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        Date dt1 = format.parse(o1.get(name).toString());
                        Date dt2 = format.parse(o2.get(name).toString());
                        
                        if (dt1.getTime() > dt2.getTime()) {
                        	
                            return 1;//小的放前面
                        }else {
                        	
                            return -1;
                        } 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return 0;
                }
            });
        }
    public static void ListSortId(List<Map<String, Object>> list,String name) {
        //用Collections这个工具类传list进来排序
        	if(list.isEmpty()) return;
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
              
                    try {
                        Long id1 = Long.parseLong(o1.get(name).toString());
                        Long id2 = Long.parseLong(o2.get(name).toString());
                        
                        if (id1 > id2) {
                        	
                            return 1;//小的放前面
                        }else {
                        	
                            return -1;
                        } 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return 0;
                }
            });
        }
    public static void ListSortIdTranspose(List<Map<String, Object>> list,String name) {
        //用Collections这个工具类传list进来排序
        	if(list.isEmpty()) return;
            Collections.sort(list, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
              
                    try {
                        Long id1 = Long.parseLong(o1.get(name).toString());
                        Long id2 = Long.parseLong(o2.get(name).toString());
                        
                        if (id1 < id2) {
                        	
                            return 1;//小的放前面
                        }else {
                        	
                            return -1;
                        } 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return 0;
                }
            });
        }
}
